package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.util.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("orderSetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    @RequestMapping("setOrderSettingByDay")
    public Result setOrderSettingByDay(@RequestBody OrderSetting orderSetting) {
        try {
            System.out.println(orderSetting.getOrderDate());
            orderSettingService.setOrderSettingByDay(orderSetting);
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("findOrderSettingByMonth")
    public Result findOrderSettingByMonth(String date) {
        try {
            if (date == null) {
                return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
            }
            List<Map> list = orderSettingService.findOderSettingByMonth(date);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    @RequestMapping("upload")
    public Result upload(MultipartFile excelFile) {
        try {
            if (excelFile == null) {
                return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
            } else {
                if (!excelFile.getOriginalFilename().endsWith("xls") && !excelFile.getOriginalFilename().endsWith("xlsx")) {
                    return new Result(false, MessageConstant.FILE_TYPE_ERROR);
                } else {
                    List<String[]> strings = POIUtils.readExcel(excelFile);

                    orderSettingService.upload(strings);
                    return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }
}
